package com.kayt.cyberBar.cuser.business.Util;

import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NumberUtil {
  private static Random rand = new Random();
  static Log log = LogFactory.getLog(NumberUtil.class);
  /**
   * 获取一个随即整数字符
   * @return
   */
  public static char getRandomNumber(){
    char a = (char)((byte)rand.nextInt(10)+48);
    return a;
  }
  /**
   * 根据传入数对现有数据进行补0
   * addzero
   * @descript 
   * @return
   */
  public static int addzero(int num,int size){
    if(size<1){
      return num;
    }
    String t = String.valueOf(num);
    if(t.length()<size){
      String temp = "";
      for(int i=t.length();i<size;i++){
        temp+="0";
      }
      t = temp+t;
      return parseInt(t);
    }
    return num;
  }
  /** 获取 A B之间的随机数
   * @param a
   * @param b
   * @return
   */
  public static int getRandom(int a, int b) {
        int temp = 0;
        try {
            if (a > b) {
                temp = rand.nextInt(a - b);
                return temp + b;
            } else {
                temp = rand.nextInt(b - a);
                return temp + a;
            }
        } catch (Exception e) {
           log.error("",e);
        }
        return temp + a;
    }
  
  /**
   * 获取一个随即字母字符(小写)
   * @return
   */
  public static char getRandomChar(){
    char a = (char)((byte)rand.nextInt(26)+97);
    return a;
  }
  

  /**
   * 给出一个长度为指定length的字符串，字符串内容为数字加小写字母
   * @param length
   * @return
   */
  public static String getRandomString(int length){
    String tmp = "";
    for(int i=0;i<length;i++){
      if(rand.nextInt(2)==0)
        tmp = tmp + getRandomNumber();
      else
        tmp = tmp + getRandomChar();
    }
    return tmp;
  }
    
    /**
     * 找出一个自然数<=给定最大值msxValue,并且这个自然数不在指定的数组中出现过，且其值为最小。
     * 当条件不符合时返回-1。
     * @param na - a number array
     * @param maxValue - max number
     * @return a number which is the min natural number and isn't in array and be <= the maxValue.
     * return -1 if and only if can't find the number corresponding to condition.
     * */
    public static int getNormalNO(int[] na,int maxValue){
       for1:
       for(int i=1;i<=maxValue;i++){
           for(int j=0;j<na.length;j++){
               if(i == na[j])
                   continue for1;
           }
           return i;
       }
       return -1;
    }

  /**
   * 得到一个int型数据
   * @param str
   * @return
   */
  public static int getInt(String str) {
    int i = 0;
    try {
      i = Integer.valueOf(str);
    } catch (Exception e) {
      log.error("",e);
    }
    return i;
  }
  
  /**
   * 得到一个ran基数的随机数
   * @param ran
   * @return
   */
  public static int getRandom(int ran){
    return rand.nextInt(ran);
  }
  public static double parseDouble(String str) {
    double re = 0;
    try {
      re = Double.parseDouble(str);
    } catch (Exception e) {
      log.error("",e);
    }
    return re;
  }

  public static int parseInt(double num) {
    int result = 0;
    String str = num + "";
    int index = str.indexOf(".");
    if (index >= 0) {
      result = parseInt(str.substring(0, index));
    } else {
      result = parseInt(str);
    }
    return result;
  }

  public static int parseInt(String str) {
    return parseInt(str, 0);
  }

  public static int parseInt(String str, int custom) {
    return parseInt(str, custom, custom);
  }

  public static int parseInt(String str, int min, int custom) {
    int result = custom;
    try {
      result = Integer.parseInt(str);
    } catch (Exception e) {
      log.error("",e);
      result = custom;
    }
    // result = Math.abs(result);
    if (result < min) {
      result = min;
    }
    return result;
  }

  public static long parseLong(String str) {
    long re = 0;
    try {
      re = Long.parseLong(str);
    } catch (Exception e) {

    }
    return re;
  }
  public static int[] toInts(String content, String split) {
    if(null ==content || content.trim().length() ==0 || null == split){
      return null;
    }
    String[] strs = content.split(split);
    if (strs.length == 0) {
      return null;
    }
    int[] re = new int[strs.length];
    for (int i = 0; i < re.length; i++) {
      re[i] = parseInt(strs[i]);
    }

    return re;
  }
  public static int[] toInts(String content) {
    return toInts(content, ",");
  }
}
